
//var baseServer="http://39.105.191.150:18899/app/v1/";
var baseServer="http://192.168.3.111:8899/app/v1/";
var reprotDownUrl="http://192.168.3.111:8080/upload/report/";


$(function(){
	// localStorage.getItem("user")
	var currentUrl = window.location.href; // 获取当前页面的 URL
	
	// 检查当前页面是否为登录页面
	var isLoginPage = currentUrl.includes("merchantlogin.html"); // 确保使用正确的 login 页面 URL

	var userId = localStorage.getItem("userId")
	// if(!userId && !isLoginPage){
	// 	window.location.href='/app/html/merchantlogin.html';
	// }
});




function getQueryString(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null){
        return unescape(decodeURI(r[2]));
    }
    return null;
}

function goBack() {
   window.history.go(-1)
}



function getCurrentTimeString() {
    const now = new Date();

    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始
    const day = String(now.getDate()).padStart(2, '0');
    // const hours = String(now.getHours()).padStart(2, '0');
    // const minutes = String(now.getMinutes()).padStart(2, '0');
    // const seconds = String(now.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day}`;
}


const CookieUtil = {
    // 设置Cookie
    set: function(name, value, daysToLive) {
        let cookie = `${name}=${encodeURIComponent(value)}`;

        if (daysToLive) {
            const date = new Date();
            date.setTime(date.getTime() + (daysToLive * 24 * 60 * 60 * 1000));
            cookie += `; expires=${date.toUTCString()}`;
        }

        cookie += '; path=/';
        document.cookie = cookie;
    },

    // 获取Cookie
    get: function(name) {
        const cookies = document.cookie.split(';');
        for (let cookie of cookies) {
            const [cookieName, cookieValue] = cookie.trim().split('=');
            if (cookieName === name) {
                return decodeURIComponent(cookieValue);
            }
        }
        return null;
    },

    // 删除Cookie
    delete: function(name) {
        this.set(name, '', -1);
    }
};

// 检查登录状态
function checkLoginStatus() {
    // 获取存储的用户类型和登录状态
    const userType = CookieUtil.get('userType');
    const isLoggedIn = CookieUtil.get('isLoggedIn') === 'true';
    const token = CookieUtil.get('authToken');

    // 如果有有效的登录状态，自动跳转
    if (isLoggedIn && token && userType) {
        // 可以添加验证token有效性的API调用
        validateToken(token, userType);
    }
}

// 验证token有效性
function validateToken(token, userType) {
    // 这里应该调用后端API验证token
    // 为了演示，这里直接使用setTimeout模拟API调用
    setTimeout(() => {
        // 假设token验证成功
        const tokenValid = true;

        if (tokenValid) {
            // 根据用户类型跳转到相应页面
            if (userType === 'merchant') {
                window.location.href = '/app/merchantlogin';
            } else if (userType === 'customer') {
                window.location.href = '/app/customersearch';
            }
        } else {
            // Token无效，清除Cookie
            CookieUtil.delete('userType');
            CookieUtil.delete('isLoggedIn');
            CookieUtil.delete('authToken');
        }
    }, 500);
}



